package JFrame;

import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import DAO.UserDaoImpl;
import DBConn.DBConn;
import MySQL.User;

public class Login extends JFrame implements ActionListener{
	private static final long serialVersionUID = -705511349178672604L;
	JLabel bg;
	JTextField t1,t2;
	CheckboxGroup type;
	Checkbox cb1,cb2;
	JButton b1;
	Connection conn;
	public Login(){
		ImageIcon bg1 = new ImageIcon(HomePage.class.getResource("userbg2.jpg"));
		ImageIcon bg2 = new ImageIcon(HomePage.class.getResource("signin.jpg"));
		bg = new JLabel(bg1);
		conn = DBConn.open();
		//e1 = new JLabel("用户名：");
		//e2 = new JLabel("密码：");
		t1 = new JTextField(10);
		t2 = new JTextField(10);
		t1.setText("");
		t2.setText("");
		type = new CheckboxGroup();
		cb1 = new Checkbox("user",true,type);
		cb2 = new Checkbox("manager",false,type);
		b1 = new JButton(bg2);
		
		//字体设置
		Font f = new Font("隶书",Font.BOLD,23);
		t1.setFont(f);
		t2.setFont(f);
		cb1.setFont(f);cb2.setFont(f);
		b1.setFont(f);
		//位置设置
		this.setLayout(null); // 绝对布局
		t1.setBounds(417, 243,226, 26);
		t2.setBounds(417, 276,226, 26);
		cb1.setBounds(420, 309,110,30); cb2.setBounds(535, 309,110,30);
		b1.setBounds(470, 340,97,48);
		bg.setBounds(0, 0, 1024, 512);
		//add(e1);  add(e2);
		add(t1);  add(t2);
		add(cb1);  add(cb2);
		add(b1);
		this.add(bg);
		
		//监听
		b1.addActionListener(this);
		
		//窗体设置
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); //关闭窗口监听
		this.setBounds(200,100,1024,512);
		setTitle("用户登录");
		setVisible(true);
		
		//setResizable(false);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		User u = new User();
		UserDaoImpl udi = new UserDaoImpl();
		try{
			u = udi.getUserById(t1.getText());
			if(t2.getText().equals(u.getPassword())){  //这里我没有加入判断用户和管理员的方法
				this.setVisible(false);   //隐藏登录窗口
				new MyFrame();
			}		
		}catch (Exception e1) {
			JOptionPane.showMessageDialog(null,"用户名或密码错误","Erro",JOptionPane.ERROR_MESSAGE);
			t1.requestFocus();
		}
	}
	public static void main(String[] args) {
		new Login();
	}
}
